package com.api.department.uploadnewdocumentattached;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.test.context.ContextConfiguration;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

import com.api.test.AbstractApiTest;
import com.api.test.AssertApi;
import com.api.test.UrnType;
import com.zyncro.api.IDepartmentApiService;
import com.zyncro.api.IWallApiService;
import com.zyncro.api.exceptions.ZyncroApiException;
import com.zyncro.api.model.ObjectResultApi;
import com.zyncro.api.model.UploadDocumentResultApi;

@ContextConfiguration(locations = "classpath:/department/department-toedit.xml")
public class UploadNewDocumentAttached extends AbstractApiTest {

	@Value("#{department_toEdit[owner].appId}")
	private String userUrn;

	@Value("#{department_toEdit[owner].password}")
	private String userPassword;

	@Value("#{department_toEdit.urn}")
	private String iddepartment;

	@Value("#{department_toEdit.folderUrn}")
	private String parentDocumentUrn;

	private String eventUrn;

	@BeforeClass
	protected void loginUser() throws Exception {

		getApi().login(userUrn, userPassword);

		this.eventUrn = getApi().getService(IWallApiService.class).publishInGroup(iddepartment, "Commentario", null, null, null, null,
				null, null, false, false, false);
	}

	@SuppressWarnings("deprecation")
	@Test
	public void test() throws ZyncroApiException, FileNotFoundException {
		String description = null;

		File file = new File("FileName.txt");
		Long length = file.length();
		String fileName = file.getName();
		FileInputStream inputStream = new FileInputStream(file);

		ObjectResultApi<UploadDocumentResultApi> result = getApi().getService(IDepartmentApiService.class).uploadNewDocumentAttached(
				iddepartment, getApi().getAccessKey(), parentDocumentUrn, eventUrn, description, fileName, length, inputStream);
		UploadDocumentResultApi doc = result.getObject();

		AssertApi.assertUrnType(doc.getDocumentUrn(), UrnType.DOCUMENT);
	}
}
